import {defineStore} from 'pinia'
import {successCode} from "~/api/base/http.js";

export const constantStore = defineStore('constant', {
    state() {
        return {
            imageUrl: "/portal/image/",
            currentActivityTab: "",
            doubleCount: "",
            webTitle: "",
            webDescription: "",
            webKeywords: "",
            webLogo: "",
            webDiscount: 0.0,
            shoppingIconShow: false,
            shoppingThisTabProjectId: null,
            stateMapping: [
                {type: "success", data: "0", msg: "正常",},
                {type: "delete", data: "1", msg: "回收站",}],
            stateOption: [
                {
                    label: "是",
                    value: "1"
                },
                {
                    label: "否",
                    value: "0"
                }
            ],
        }
    },
    actions: {
        formatNumber(num, digits) {
            // 检查是否是整数
            if (Number.isInteger(num)) {
                return num;
            }
            // 使用 toFixed 方法保留小数
            let formatted = num.toFixed(digits);
            // 如果小数部分全是 0，返回整数部分
            if (formatted.indexOf('.') !== -1 && parseFloat(formatted) === Math.floor(num)) {
                return Math.floor(num);
            }
            // 否则返回保留指定位数的小数
            return parseFloat(formatted);
        },
        setShoppingThisTabProjectId(flat) {
            this.shoppingThisTabProjectId = flat;
        },
        setShoppingIconShow(flat) {
            this.shoppingIconShow = flat;
        },
        setWebInfo(info) {
            this.webTitle = info["web_title"];
            this.webDescription = info["web_description"];
            this.webKeywords = info["web_keywords"];
            this.webLogo = info["web_logo"];
            let infoElement = info["web_discount"];
            this.webDiscount = infoElement === "" || infoElement === undefined || infoElement === null ? -1 : parseFloat(infoElement);
        },
        checkPasswordStrength(password) {
            // 默认密码强度为空
            let power = -1;
            if (/[0-9]/.test(password) && /[a-z]/.test(password) && /[A-Z]/.test(password) && /[^a-zA-Z0-9]/.test(password)) {
                // 如果密码包含数字、小写字母、大写字母和特殊字符中的至少三种类型，强度提升为强
                return 2;
            } else if (password.length >= 8) {// 如果密码长度大于等于8个字符，强度提升为中
                power = 1;
            } else if (password !== "") {
                power = 0;
            }
            return power;
        },

        calc(isBluePic, multiplier, multiplierUp, level, levelUp, price, number, discount = null) {
            let bluePicMoney = isBluePic ? 2 : 1;
            let multiplierMoney = (multiplier) * (multiplierUp && multiplierUp > 0 ? multiplierUp : 0);
            let levelMoney = (level - 1) * (levelUp && levelUp > 0 ? levelUp : 0);
            let calcDisc = this.calcDiscount(discount);
            let disCount = calcDisc > -1 ? calcDisc * 0.1 : 1;
            return (((price + multiplierMoney + levelMoney) * number * bluePicMoney) * disCount).toFixed(4);
        },
        calcDiscount(discount) {
            let itemDiscount = discount === "" || discount === null || discount === undefined ? -1 : parseFloat(discount);
            let result = -1.0
            if (this.webDiscount > 0 && itemDiscount > 0) {
                result = (this.webDiscount * itemDiscount / 10).toFixed(2);
            } else if (this.webDiscount > 0 && itemDiscount <= 0) {
                result = (this.webDiscount).toFixed(1);
            } else if (itemDiscount > 0 && this.webDiscount <= 0) {
                result = (itemDiscount).toFixed(1);
            } else if (this.webDiscount <= 0 && itemDiscount <= 0) {
                result = -1.0
            }
            return result;
        },
        isClient() {
            return typeof process !== 'undefined' && process.client;
        },
        itemIsNull(item) {
            return item === '' || item === null || item === undefined
        },
        copyPage(pageObj, resData) {
            pageObj.value.currentPage = resData.page;
            pageObj.value.pageSize = resData.size;
            pageObj.value.totalPages = resData.totalPages;
            pageObj.value.totalCount = resData.totalCount;
            pageObj.value.first = resData.first;
            pageObj.value.last = resData.last;
        },

        listToSelect(dataList, label, value) {
            let select = [];
            for (let i = 0; i < dataList.length; i++) {
                let obj = {
                    label: dataList[i][label],
                    value: dataList[i][value]
                }
                select.push(obj);
            }
            return select;
        },

        setCurrentActivityTab(currentActivityTab) {
            this.currentActivityTab = currentActivityTab;
        },

        sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        },

        parseRes(res) {
            if (res !== null && res.value.code === successCode) {
                return res.value.data
            } else {
                return ""
            }
        }

    }
});